<template>
  <el-container style="height: 700px; border: 1px solid #eee">
    <el-aside width="200px" style="background-color: rgb(238, 241, 246)">
      <el-menu :default-openeds="['1', '3']">
        <el-submenu index="1">
          <template slot="title"
            ><i class="el-icon-message"></i>导航一</template
          >
          <el-menu-item-group>
            <template slot="title">分组一</template>
            <el-menu-item index="1-1" @click="addTab('首页','/search')"
              >首页</el-menu-item
            >
            <el-menu-item index="1-2" @click="addTab('选择','/chose')">选择</el-menu-item>
          </el-menu-item-group>
          <el-menu-item-group title="分组2">
            <el-menu-item index="1-3">选项3</el-menu-item>
          </el-menu-item-group>
          <el-submenu index="1-4">
            <template slot="title">选项4</template>
            <el-menu-item index="1-4-1">选项4-1</el-menu-item>
          </el-submenu>
        </el-submenu>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header>
        <Header></Header>
      </el-header>

      <el-main>
        <div style="margin-bottom: 5px;">
          <el-tabs
            v-model="editableTabsValue"
            type="card"
            closable
            @tab-remove="removeTab"
            @tab-click="clickTab"
          >
            <el-tab-pane
              v-for="(item, index) in editableTabs"
              :key="item.name"
              :label="item.title"
              :name="item.name"
            >
            <router-view></router-view>
            </el-tab-pane>
          </el-tabs>
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<style>
.el-header {
  background-color: #b3c0d1;
  color: #333;
  line-height: 60px;
}
.el-aside {
  color: #333;
}
.el-main {
  padding: 5px;
}
</style>

<script>
import Header from "@/components/home/Header";
export default {
  data() {
    const item = {
      date: "2016-05-02",
      name: "王小虎",
      address: "上海市普陀区金沙江路 1518 弄"
    };
    return {
      editableTabsValue: "1",
      editableTabs: [],
      tabIndex: 1
    };
  },
  methods: {
    //校验tabs页是否已存在
    checkTabIsExist(targetName){
      let result = true;
        this.editableTabs.forEach((index)=>{
          if(index.title === targetName){
            result = false;
          }
        })
      return result;
    },
    //增加tab页
    addTab(targetName,url) {
      if(this.checkTabIsExist(targetName)){
        //let newTabName = ++this.tabIndex + "";
        this.editableTabs.push({
        title: targetName,
        name: targetName,
        content: "New Tab content",
        url: url
        });
        this.editableTabsValue = targetName;
      }else {
        this.editableTabsValue = targetName;
      }
      this.$router.push(url);
    },
    //删除tab页
    removeTab(targetName) {
      let tabs = this.editableTabs;
      let activeName = this.editableTabsValue;
      if (activeName === targetName) {
        tabs.forEach((tab, index) => {
          if (tab.name === targetName) {
            let nextTab = tabs[index + 1] || tabs[index - 1];
            if (nextTab) {
              activeName = nextTab.name;
              this.$router.push(nextTab.url);
            }
          }
        });
      }
      this.editableTabsValue = activeName;
      this.editableTabs = tabs.filter(tab => tab.name !== targetName);
    },
    //监听被选中的tab页
    clickTab(tab){
      let tabs = this.editableTabs;
      tabs.forEach((item) => {
        if(item.title === tab.label){
          console.log(item.url)
          this.$router.push(item.url);
        }
      })
    }
  },
  components: {
    Header
  }
};
</script>

<style>
a {
    text-decoration: none;
}
</style>
